﻿<%@ WebHandler Language="C#" Class="GetProjectData" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Collections;
using System.Web.Script.Serialization;

public class GetProjectData : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/json";
        string strName = context.Request["name"];
        JavaScriptSerializer js = new JavaScriptSerializer();
        if (context.Session["BRANCH_ID"] != null)
        {
            string strBranchID = context.Session["BRANCH_ID"].ToString();
            if (strName.Equals("all"))
            {
                context.Response.Write(js.Serialize(GetAllContent(strBranchID)));
            }
            else
            {
                context.Response.Write(js.Serialize(GetAllContentByName(strName, strBranchID)));
            }
        }
        else
        {
            context.Response.Write("SessionTimeout");
        }
    }

    private ArrayList GetAllContent(string strBranchID)
    {
        ArrayList modelList = new ArrayList();
        DataTable dt = new DataTable();
        BLL.Progect.T_PUB_PROJECT bll = new BLL.Progect.T_PUB_PROJECT();
        dt = bll.GetProjectName().Tables[0];
        int j = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Name name = new Name();
            name.id = (j++).ToString();
            name.s = dt.Rows[i][0].ToString();
            modelList.Add(name);
            if (j > 26) break;
        }
        return modelList;
    }

    private ArrayList GetAllContentByName(string strName, string strBranchID)
    {
        string s;
        ArrayList modelList = new ArrayList();
        DataTable dt = new DataTable();
        BLL.Progect.T_PUB_PROJECT bll = new BLL.Progect.T_PUB_PROJECT();
        dt = bll.GetProjectName().Tables[0];
        int j = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            s = dt.Rows[i][0].ToString();
            if (s.StartsWith(strName))
            {
                Name name = new Name();
                name.id = (j++).ToString();
                name.s = dt.Rows[i][0].ToString();
                modelList.Add(name);
                if (j > 26) break;
            }        
        }
        return modelList;
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

    public class Name
    {
        public string s = "";
        public string id = "";
    }
}